﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Threading;
using System.Web.Mvc;
using AutoMapper;
using SoftMedia.Opcion.Web.Models;
using SoftMedia.Opcion.Web.OpcionService;
using Telerik.Web.Mvc;
using Telerik.Web.Mvc.UI;

namespace SoftMedia.Opcion.Web.Controllers
{
    public class AsambleaController : Controller
    {
        //CONTS_ADJ: cuando el valor sea cero 0
        //MESES_TRANSC: cuando el valor sea cero 0
        //PROXIMA_REUNION_DIA: Cuando el valor sea “01/01/1900”

        public class proxasamResolver : ValueResolver<ResultadoAsamblea_Result, string>
        {
            protected override string ResolveCore(ResultadoAsamblea_Result source)
            {
                return source.proxasam.Value.Year.Equals(1900) ? "" : String.Format("{0:dddd, dd 'de' MMMM}", source.proxasam);
            }
        }

        public class mesestransResolver : ValueResolver<ResultadoAsamblea_Result, string>
        {
            protected override string ResolveCore(ResultadoAsamblea_Result source)
            {
                return source.mesestrans.Value == 0 ? "" : source.mesestrans.ToString();
            }
        }

        public class contratosadjResolver : ValueResolver<ResultadoAsamblea_Result, string>
        {
            protected override string ResolveCore(ResultadoAsamblea_Result source)
            {
                return source.contratosadj == 0 ? "" : source.contratosadj.ToString();
            }
        }

        public class ofertaremateResolver : ValueResolver<ResultadoAsamblea_Result, string>
        {
            protected override string ResolveCore(ResultadoAsamblea_Result source)
            {
                return source.ofertaremate == 0 ? "" : source.ofertaremate.ToString();
            }
        }
        //
        // GET: /Asamblea/

        public IEnumerable<AsambleaModel> ResultSetMapper(List<ResultadoAsamblea_Result> rs)
        {
            Mapper.CreateMap<ResultadoAsamblea_Result, AsambleaModel>()
            .ForMember(c => c.mesestrans, o => o.ResolveUsing<mesestransResolver>())
            .ForMember(c => c.contratosadj, o => o.ResolveUsing<contratosadjResolver>())
            .ForMember(c => c.ofertaremate, o => o.ResolveUsing<ofertaremateResolver>())
            .ForMember(c => c.proxasam, o => o.ResolveUsing<proxasamResolver>());

            Mapper.AssertConfigurationIsValid();

            var resultado = Mapper.Map<IEnumerable<ResultadoAsamblea_Result>, IEnumerable<AsambleaModel>>(rs);

            return resultado;
        }

        public List<ResultadoAsamblea_Result> ObtainDataWS(string id)
        {
            OpcionService.OpcionClient svc = new OpcionClient();
            List<ResultadoAsamblea_Result> rs = svc.GetResultadoAsamblea(id).ToList();
            return rs;
        }

        public IEnumerable<AsambleaModel> GetArDTO(string id, int os)
        {
            return ResultSetMapper(ObtainDataWS(id).Where(c => c.ordensemana == os).ToList());
        }

        public ActionResult Resultado(string id)
        {
            OpcionService.OpcionClient svc = new OpcionClient();
            string periodo;
            periodo = id == null ? svc.GetAsambleaResult().First().periodo : id;

            var columns = new[] { 
                new GridColumnSettings { Member = "mesestrans", Title = "MESES TRANSC." , Width = "30px", }
                };

            var config = new Dictionary<string, int> 
                    { 
                        { "GridWidth", 0 },
                        { "mesestrans", 40 }      
                    };

            ViewData["Columns"] = columns;
            ViewData["config"] = config;

            var cultura = new CultureInfo("es-ES");

            Thread.CurrentThread.CurrentUICulture = cultura;
            Thread.CurrentThread.CurrentCulture = cultura;
            var fecha = DateTime.ParseExact(periodo, "yyyyMM", null);

            Session["Month"] = fecha.ToString("MMMM").ToUpper(new CultureInfo("es-ES"));
            Session["Year"] = fecha.Year.ToString();

            var rs1 = GetArDTO(id, 1);
            ViewData["grid1db"] = rs1;
            ViewBag.count1 = rs1.Count();

            var rs2 = GetArDTO(id, 2);
            ViewData["grid2db"] = rs2;
            ViewBag.count2 = rs2.Count();

            var rs3 = GetArDTO(id, 3);
            ViewData["grid3db"] = rs3;
            ViewBag.count3 = rs3.Count();

            var rs4 = GetArDTO(id, 4);
            ViewData["grid4db"] = rs4;
            ViewBag.count4 = rs4.Count();

            return View("_Resultado");
        }

        [GridAction]
        public ActionResult _ajaxAsambleaBinding1(string id)
        {
            var rs = GetArDTO(id, 1);
            ViewData["grid1db"] = rs;
            return PartialView("_Resultado", new GridModel<AsambleaModel> { Data = rs });
        }

        [GridAction]
        public ActionResult _ajaxAsambleaBinding2(string id)
        {
            var rs = GetArDTO(id, 2);
            ViewData["grid2db"] = rs;
            return PartialView("_Resultado", new GridModel<AsambleaModel> { Data = rs });
        }

        [GridAction]
        public ActionResult _ajaxAsambleaBinding3(string id)
        {
            var rs = GetArDTO(id, 3);
            ViewData["grid3db"] = rs;
            return PartialView("_Resultado", new GridModel<AsambleaModel> { Data = rs });
        }

        [GridAction]
        public ActionResult _ajaxAsambleaBinding4(string id)
        {
            var rs = GetArDTO(id, 4);
            ViewData["grid4db"] = rs;
            return PartialView("_Resultado", new GridModel<AsambleaModel> { Data = rs });
        }

    }

}
